package category_big.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.DecoratedStackPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class category_big_old implements EntryPoint {
	VerticalPanel categoryPanel;
	ProductList pl;
	public void onModuleLoad() {
		categoryPanel = getCategoryPanel();
		categoryPanel.setStyleName("categoryPanel");
		pl = new ProductList("?category=0&lang=big");
		RootPanel.get("category").add(categoryPanel);
		RootPanel.get("productlist").add(pl);
	}

	protected VerticalPanel getCategoryPanel(){
	    DecoratedStackPanel stackPanel = new DecoratedStackPanel();
	    stackPanel.setWidth("200px");
		VerticalPanel vp = new VerticalPanel();
		vp.add(stackPanel);
		getCategoryArray(stackPanel);
	    return vp;
	}
	  private Widget createFiltersItem(ArrayList<Anchor> arr ) {
		    VerticalPanel filtersPanel = new VerticalPanel();
		    filtersPanel.setSpacing(4);
		    for(Anchor s:arr){
		    filtersPanel.add(s);
		    }
		    return new SimplePanel(filtersPanel);
		  }
	  private void getCategoryArray(DecoratedStackPanel stackPanel){
		  stackPanel.clear();
//		  ArrayList<Anchor> a0 = new ArrayList<Anchor>();
//		  Anchor category0 = new Anchor("全部產品");
//		  category0.addClickHandler(new ClickHandler(){
//			public void onClick(ClickEvent event) {
//				pl.query="?category=0&lang=big";
//				pl.getProductsMap();
//				
//			}});
//		  Anchor category1 = new Anchor("最新產品");
//		  category1.addClickHandler(new ClickHandler(){
//				public void onClick(ClickEvent event) {
//					pl.query="?category=1&lang=big";
//					pl.getProductsMap();
//					
//				}});
//		  Anchor category2 = new Anchor("暢銷產品");
//		  category2.addClickHandler(new ClickHandler(){
//				public void onClick(ClickEvent event) {
//					pl.query="?category=2&lang=big";
//					pl.getProductsMap();
//					
//				}});		  
//		  a0.add(category0);a0.add(category1);a0.add(category2);
//		  stackPanel.add(createFiltersItem(a0),"產品展示");
		  String url = GWT.getModuleBaseURL() + URL.encode("getCategoryService?lang=big");
		  RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
		  try {
			builder.sendRequest(null, new GetContentRequestCallback(stackPanel));
		  } catch (RequestException e) {
		  }
	  }
	  
	  private class GetContentRequestCallback implements RequestCallback {
		  DecoratedStackPanel sp;
			public GetContentRequestCallback(DecoratedStackPanel stackPanel) {
				super();
				sp = stackPanel;
			}
			public void onError(Request request, Throwable exception) {
			}
			public void onResponseReceived(Request request, Response response) {
			   if (200 == response.getStatusCode()) {
			    	  String respText = response.getText();
			    	  if(respText.equals(""))return;
			    	  JSONValue respValue = JSONParser.parseLenient(respText);
			    	  JSONArray receivedArr = respValue.isArray();
					  for(int i=0;i<receivedArr.size();i++){
							JSONObject jo = receivedArr.get(i).isObject();
							String catName = jo.get("CatName").isString().stringValue();
							JSONArray catArray = jo.get("CatArray").isArray();
							 final ArrayList<Anchor> a0 = new ArrayList<Anchor>();
							 for(int ii=0;ii<catArray.size();ii++){
								   JSONObject element = catArray.get(ii).isObject();
									double tempd = element.get("CatID").isNumber().doubleValue();
									final String id = new Integer((int)tempd).toString();
									final String value = element.get("SubCategory").isString().stringValue();
									final String query = "<span style='font-size:12;'>"+value+"</span>";
									final Anchor anchor = new Anchor();
									anchor.setHTML(query);
									anchor.addClickHandler(new ClickHandler(){
									public void onClick(ClickEvent event) {
										formatAllCatAnchor(sp);
										anchor.setHTML("<font color='red'>"+query+"</font>");
										pl.query = "?category="+id+"&lang=big";
										pl.getProductsMap();
									}});
								 a0.add(anchor);
							 }
							 sp.add(createFiltersItem(a0), catName,true);
						}
		      } 
			}
		}
	  private void formatAllCatAnchor(DecoratedStackPanel ds){
		  int widgetCount = ds.getWidgetCount();
		  for(int i=0;i<widgetCount;i++){
			  SimplePanel sp = (SimplePanel)ds.getWidget(i);
			  VerticalPanel vp= (VerticalPanel)sp.getWidget();
			  int anchorCount = vp.getWidgetCount();
			  for(int j=0;j<anchorCount;j++){
				  Anchor target = (Anchor)vp.getWidget(j);
				  target.setHTML("<span style='font-size:12;'>"+target.getText()+"</span>");
			  }
		  }
	  }
}
